#include "graph-node.h"
#include "section-graph-creator.h"
#include "section-info.h"

SectionGraphCreator::SectionGraphCreator(QObject *parent) :
    QObject(parent)
{
}

GraphNode<SectionInfo>* SectionGraphCreator::createBeginningGraph()
{
    return createNode(SmGlobal::ServerSection, "Serwery", QSharedPointer<AbstractDatabaseClient>());
}

void SectionGraphCreator::createDatabaseGraph (GraphNode<SectionInfo>* rootNode, QSharedPointer<AbstractDatabaseClient> databaseClient)
{
    rootNode->deleteChildNodes();

    // children of server node
    GraphNode<SectionInfo>* warehouseNode = createNode(SmGlobal::WarehouseSection, "Magazyny", databaseClient);
    GraphNode<SectionInfo>* workerNode = createNode(SmGlobal::WorkerSection, "Pracownicy", databaseClient);
    GraphNode<SectionInfo>* contractorNode = createNode(SmGlobal::ContractorSection, "Kontrahenci", databaseClient);
    GraphNode<SectionInfo>* productNode = createNode(SmGlobal::ProductSection, "Produkty", databaseClient);
    GraphNode<SectionInfo>* orderNode = createNode(SmGlobal::OrderSection, "Zamówienia", databaseClient);
    rootNode->addChildNode(warehouseNode);
    rootNode->addChildNode(workerNode);
    rootNode->addChildNode(contractorNode);
    rootNode->addChildNode(productNode);
    rootNode->addChildNode(orderNode);

    // children of warehouse node
    GraphNode<SectionInfo>* productInWarehouseNode = createNode(SmGlobal::ProductInWarehouseSection, "Produkty w magazynie", databaseClient);
    warehouseNode->addChildNode(productInWarehouseNode);

    // children of order node
    GraphNode<SectionInfo>* orderItemNode = createNode(SmGlobal::OrderItemSection, "Pozycje zamówienia", databaseClient);
    orderNode->addChildNode(orderItemNode);
}

GraphNode<SectionInfo>* SectionGraphCreator::createNode (SmGlobal::SectionType section, const QString& description,
                                                         QSharedPointer<AbstractDatabaseClient> databaseClient,
                                                         GraphNode<SectionInfo>* parentNode)
{
    GraphNode<SectionInfo>* node = new GraphNode<SectionInfo>(parentNode);
    node->data().setSection(section);
    node->data().setDescription(description);
    node->data().setDatabaseClient(databaseClient);
    return node;
}
